/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package connection;

import authentication.EncryptionType;
import data.AccountData;
import data.Data;
import data.email.EmailFolder;
import data.email.EmailMessage;
import java.util.LinkedList;
import java.util.List;
import java.util.Properties;
import javax.mail.Folder;
import javax.mail.Session;
import javax.mail.Store;

//import javax.
/**
 *
 * @author Arcanis
 */
public class Pop3Account extends Account implements IReceiver {
    //@TODO Necka - POP3 Account

    Pop3Account(AccountData data) throws Exception {
        super(data);

        createStore();
    }
    protected Store mailStore = null;
    private EmailFolder inbox = null;

    private boolean createStore() throws Exception {
        Properties props = new Properties();
        String proto = null;
        if (this.encryption.equals(EncryptionType.none)) {
            proto = "pop3";
        } else {
            proto = "pop3";
            //Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
            props.setProperty("mail.pop3.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
            props.setProperty("mail.pop3.socketFactory.fallback", "false");
            props.setProperty("mail.pop3.port", "995");
            props.setProperty("mail.pop3.socketFactory.port", "995");
        }

        props.put("mail.store.protocol", proto);
        /*
         * props.setProperty("mail.imaps.partialfetch", "false");
         */
        Session session = Session.getDefaultInstance(props);
        try {
            this.mailStore = session.getStore(proto);
        } catch (Exception e) {
            throw e;
        }
        return true;
    }

    public boolean connectToStore() {
        if (this.mailStore != null && !this.mailStore.isConnected()) {
            try {
                this.mailStore.connect(this.serverName, this.port, this.authenticationData.getLogin(), this.authenticationData.getPassword());
                return true;
            } catch (Exception e) {
                //@TODO what
            }
        }
        return false;
    }

    public void disconnectFromStore() {
        if (this.mailStore != null && this.mailStore.isConnected()) {
            try {
                this.mailStore.close();
            } catch (Exception e) {
                // @TODO wtf
            }
        }
    }

    @Override
    public List<EmailFolder> loadAllFolders() throws Exception {
        this.connectToStore();

        //Folder defaultFolder = mailStore.getDefaultFolder();
        Folder defaultFolder = mailStore.getFolder("INBOX");
        if (defaultFolder != null) {
            this.inbox = new EmailFolder(defaultFolder, this);
            List<EmailFolder> l = new LinkedList<>();
            l.add(inbox);
            return l;
        }

        return null;
    }

    @Override
    public void updateMessages(Data data) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public List<EmailFolder> getAllCachedFolders() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public EmailFolder getEmailFolder(String folderName) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public EmailFolder getEmailFolder(int number) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void deleteFolder(String folderName) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void deleteFolder(int number) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}
